//
//  XTNewFeatureCollectionViewController.swift
//  HrenryWeibo
//
//  Created by 熊涛 on 16/5/14.
//  Copyright © 2016年 熊涛. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class XTNewFeatureCollectionViewController: UICollectionViewController {

    /// layout
    let flowLayout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.collectionView!.registerClass(XTNewFeatureCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        //设置layout属性
        setLaout()
    }

    //设置layout属性
    func setLaout() {
    
        //item大小
        flowLayout.itemSize = UIScreen.mainScreen().bounds.size
        //横向滑动
        flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //item之间的间隙
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
        //翻页的模式 -- 一页一页的翻
        collectionView?.pagingEnabled = true
        //弹簧效果
        collectionView?.bounces = false
        
    }
    
    
    init() {
        super.init(collectionViewLayout: flowLayout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //MARK: - 数据源方法
    //多少行
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return NewFeaturePage
    }
    
    //cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        //dequeueReusableCellWithReuseIdentifier : 取cell会先去缓存池中寻找,缓存池中找不到就会使用注册的cell来创建
       let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! XTNewFeatureCollectionViewCell
//        cell.backgroundColor = UIColor.redColor()
        cell.index = indexPath.item
        cell.callBack = {
            //回调 点击按钮就跳转控制器
            Delegate.changeRootViewController(XTMainTabBarController())
            
        }
        return cell
    }

    //MARK: - scrollviewdeleagte
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
//        XTPrint(items: "滑动停止")
        //计算当前在哪一页::::计算偏移值
        let index = scrollView.contentOffset.x / scrollView.bounds.width
        //如果是最后一页,就显示button,有动画
        if Int(index) == NewFeaturePage - 1 {
            XTPrint(items: "这个到了最后一页")
           let indexPath = NSIndexPath(forItem: NewFeaturePage - 1, inSection: 0)
           let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! XTNewFeatureCollectionViewCell
            cell.showStartBtnAndAnimation()
        }
    }

    
}
